package com.vcms.view.simulation.system;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto
//
//

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import com.vcms.controller.system.MainController;
import com.vcms.controller.system.SimulationController;
import com.vcms.view.simulation.uicomponent.listener.ActivateCustomerPanelButtonListener;
import com.vcms.view.simulation.uicomponent.listener.ActivateMachineryPanelButtonListener;
import com.vcms.view.simulation.uicomponent.listener.ActivateMaintainerPanelButtonListener;
import com.vcms.view.simulation.uicomponent.listener.BeginSimulationButtonListener;
import com.vcms.view.simulation.uicomponent.listener.EndSimulationButtonListener;

@SuppressWarnings("serial")
public class SimulatorControlPanel extends Frame {

	private static final String title = "Simulation Control Panel";

	public static final char BEGIN = 0;
	public static final char END = 1;
	public static final char ACT_CUSTOMER = 2;
	public static final char ACT_CUSTOMER_NUMERIC = 3;
	public static final char ACT_CUSTOMER_ALPHANUMERIC = 4;
	public static final char ACT_MAINTAINER = 5;
	public static final char ACT_MACHINERY = 6;
	public static final char ACT_EXIT = 7;
	private MainController mainCtrl;

	private static final String labels[] = { "Begin Simulation",
			"End Simulation", "Activate Customer Panel",
			"Activate Customer Panel (Numeric)", "Activate Customer Panel (Alphanumeric)",
			"Activate Maintainer Panel", "Activate Machinery Panel"
	// "Exit"
	};

	private Button buttons[] = new Button[labels.length];
	private Font titleFont = new Font("Helvetica", Font.BOLD, 18);
	private Font buttonFont = new Font("Helvetica", Font.BOLD, 14);

	private SimulationController simulationCtrl;

	public SimulatorControlPanel(SimulationController cont) {
		super(title);

		simulationCtrl = cont;
		mainCtrl = simulationCtrl.getMainController();

		for (int i = 0; i < labels.length; i++) {
			createButton(i, cont);
		}

		setLayout(new GridLayout(0, 1));

		add(createPanelLabel());
		add(buttons[BEGIN]);
		add(buttons[ACT_CUSTOMER]);
		add(buttons[ACT_CUSTOMER_NUMERIC]);
		add(buttons[ACT_CUSTOMER_ALPHANUMERIC]);
		add(buttons[ACT_MAINTAINER]);
		add(buttons[ACT_MACHINERY]);
		add(buttons[END]);
		// add (buttons[ACT_EXIT]);

		// setSize(200, 200);
		pack();
		setLocation(100, 100);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				simulationCtrl.getMainController().closeDown();
			}
		});
	}

	public void display() {
		this.setVisible(true);
	}

	private Label createPanelLabel() {
		Label l = new Label(title);
		l.setBackground(Color.blue);
		l.setForeground(Color.white);
		l.setFont(titleFont);
		return (l);
	}

	private void createButton(int id, SimulationController cont) {
		Button b = new Button(labels[id]);
		b.setBackground(Color.white);
		b.setForeground(Color.blue);
		b.setFont(buttonFont);
		switch (id) {
		case SimulatorControlPanel.BEGIN:
			BeginSimulationButtonListener bl;
			bl = new BeginSimulationButtonListener(simulationCtrl);
			b.addActionListener(bl);
			break;
		case SimulatorControlPanel.END:
			EndSimulationButtonListener el;
			el = new EndSimulationButtonListener(mainCtrl);
			b.addActionListener(el);
			break;
		case SimulatorControlPanel.ACT_MAINTAINER:
			ActivateMaintainerPanelButtonListener ml;
			ml = new ActivateMaintainerPanelButtonListener(simulationCtrl);
			b.addActionListener(ml);
			break;
		case SimulatorControlPanel.ACT_MACHINERY:
			ActivateMachineryPanelButtonListener l;
			l = new ActivateMachineryPanelButtonListener(simulationCtrl);
			b.addActionListener(l);
			break;
		case SimulatorControlPanel.ACT_CUSTOMER:
			ActivateCustomerPanelButtonListener custL;
			custL = new ActivateCustomerPanelButtonListener(simulationCtrl);
			b.addActionListener(custL);
			b.setActionCommand(String.valueOf(id));
			break;
		case SimulatorControlPanel.ACT_CUSTOMER_NUMERIC:
			ActivateCustomerPanelButtonListener custLN;
			custLN = new ActivateCustomerPanelButtonListener(simulationCtrl);
			b.addActionListener(custLN);
			b.setActionCommand(String.valueOf(id));
			break;
		case SimulatorControlPanel.ACT_CUSTOMER_ALPHANUMERIC:
			ActivateCustomerPanelButtonListener custLAN;
			custLAN = new ActivateCustomerPanelButtonListener(simulationCtrl);
			b.addActionListener(custLAN);
			b.setActionCommand(String.valueOf(id));
			break;
		}
		buttons[id] = b;
	}

	public void setSimulationActive(boolean isOn) {
		buttons[BEGIN].setEnabled(!isOn);
		buttons[ACT_MAINTAINER].setEnabled(isOn);
		buttons[ACT_CUSTOMER].setEnabled(isOn);
		buttons[ACT_CUSTOMER_NUMERIC].setEnabled(isOn);
		buttons[ACT_CUSTOMER_ALPHANUMERIC].setEnabled(isOn);
		buttons[ACT_MACHINERY].setEnabled(isOn);
		buttons[END].setEnabled(isOn);
	}

	public void setButtonState(char id, boolean state) {
		buttons[id].setEnabled(state);
	}

	public void closeDown() {
		dispose();
	}

	public void stop() {
		mainCtrl.closeDown();
	}

	public void setActive(char cmp, boolean st) {
		setButtonState(cmp, st);
	}
}